package com.edward.weather.strategy.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.edward.weather.model.WeatherData;
import com.edward.weather.strategy.WeatherApiStrategy;
import com.edward.weather.vo.request.WeatherRequestVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("gaode")
public class GaodeWeatherStrategy implements WeatherApiStrategy {

    @Value("${gaode.api.url}")
    private String apiUrl;

    @Override
    public WeatherData getWeather(String city) {
        String url = String.format(apiUrl, city);
        HttpResponse httpResponse = HttpRequest.get(url).execute();
        String responseStr = httpResponse.body();
        return parseWeatherData(responseStr);
    }

    @Override
    public WeatherData dayWeather(WeatherRequestVO weatherRequestVO) {
        return null;
    }

    @Override
    public WeatherData parseWeatherData(String responseStr) {
//        JSONObject jsonObject = JSONUtil.parseObj(responseStr);
//        WeatherData weatherData = new WeatherData();
//
//        //预报
//        JSONArray forecast = jsonObject.getJSONArray("forecast");
//        JSONObject forecastObj = forecast.get(0, JSONObject.class);
//        weatherData.setCity(forecastObj.getStr("province"));
//        JSONArray jsonArray = forecastObj.getJSONArray("casts");
//        JSONObject castsMap = jsonArray.getJSONObject(0);
//
//        weatherData.setTempMin(castsMap.getDouble("daytemp"));
//        weatherData.setTempMax(castsMap.getDouble("nighttemp"));

        return null;
    }
}